<?php
namespace app\member\controller\sys_admin;
use app\member\model\RegionModel;
use think\Db;

use app\AdminController;
use app\member\model\WithdrawModel;
use app\member\model\WithdrawAccountModel;
use app\member\model\UsersModel;
use app\member\model\AccountLogModel;
use think\db\Where;

//*------------------------------------------------------ */
//-- 区域代理审核
/*------------------------------------------------------ */
class Region extends AdminController
{
	 //*------------------------------------------------------ */
	//-- 初始化
	/*------------------------------------------------------ */
   public function initialize(){	
   		parent::initialize();
		$this->Model = new RegionModel();
    }
	/*------------------------------------------------------ */
	//-- 主页
	/*------------------------------------------------------ */
    public function index(){
        $data = Db::name('user_region')->select();

            foreach($data as $k1 =>$v1){
                    if($v1['order_id']){
                        if(Db::table('shop_order_info')->where('order_id',$v1['order_id'])->where('pay_status',1)->count('order_id')){
                            $v1['order'] = 1;
                        }else{
                            $v1['order'] = 2;
                        }
                    }else{
                        $v1['order'] = 0;
                    }
                    if($v1['level'] == 1){
                        $v1['city_name'] = '省代';
                        $v1['region_name'] = Db::name('bs_province')->where('PROVINCE_CODE',$v1['region'])->value('PROVINCE_NAME');
                    }elseif($v1['level']  == 2){
                        $v1['city_name'] = '市代';
                        $v1['region_name'] = Db::name('bs_city')->where('CITY_CODE',$v1['region'])->value('CITY_NAME');
                    }elseif($v1['level']  == 3){
                        $v1['city_name'] = '区代';
                        $v1['region_name'] = Db::name('bs_area')->where('AREA_CODE',$v1['region'])->value('AREA_NAME');
                    }elseif($v1['level']  == 4){
                        $v1['city_name'] = '镇代';
                        $v1['region_name'] = Db::name('bs_street')->where('STREET_CODE',$v1['region'])->value('STREET_NAME');
                    }
                    $data[$k1] = $v1;
            }
		$this->assign("data", $data);
		return $this->fetch();
	}
   /*------------------------------------------------------ */
    //-- 获取列表
	//-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false) {

        $where = [];
        $status = input('status');
        if($status != 'all'){
            $where[] = ['status','=',$status] ;
        }
       
        $data = Db::name('user_region')->where($where)->select();
        foreach($data as $k1 =>$v1){
            if($v1['level'] == 1){
                $v1['city_name'] = '省代';
                $v1['region_name'] = Db::name('bs_province')->where('PROVINCE_CODE',$v1['region'])->value('PROVINCE_NAME');
            }elseif($v1['level']  == 2){
                $v1['city_name'] = '市代';
                $v1['region_name'] = Db::name('bs_city')->where('CITY_CODE',$v1['region'])->value('CITY_NAME');
            }elseif($v1['level']  == 3){
                $v1['city_name'] = '区代';
                $v1['region_name'] = Db::name('bs_area')->where('AREA_CODE',$v1['region'])->value('AREA_NAME');
            }elseif($v1['level']  == 4){
                $v1['city_name'] = '镇代';
                $v1['region_name'] = Db::name('bs_street')->where('STREET_CODE',$v1['region'])->value('STREET_NAME');
            }
            $data[$k1] = $v1;
    }
        $this->assign("data", $data);
        if ($runData == false) {
            $data['content'] = $this->fetch('sys_admin/region/list');
            return $this->success('', '', $data);
        }
        return true;
    }

	/*------------------------------------------------------ */
	//-- 信息页调用
	//-- $data array 自动读取对应的数据
	/*------------------------------------------------------ */
	public function Info(){
        $id = input('id');
        $user_id = input('user_id');
        $region = input('region');
        if($this->request->isPost()){
            $data2 = input('');
            $proxy = Db::name('user_region')->where(['id'=>$data2['id'],'status'=>1])->value('id');
            if($proxy) return $this->error('该代理已存在.');

            $changedata['id'] = $data2['id'];
            $changedata['status'] = $data2['status'];
            $changedata['update_time'] = time();

            $res = Db::name('user_region')->where('id',$data2['id'])->update($changedata);
            $re = 1;
            if($res && $changedata['status'] == 1){
                $proxy = Db::name('users')->where('proxy',$region)->value('proxy');
                if($proxy) return $this->error('该代理已存在.');
                $re = Db::name('users')->where('user_id',$user_id)->update(['proxy'=>$region]);
            }

            if ($res !== false && $re !== false) {
                return $this->success('操作成功.',url('index'));
            }else{
                Db::rollback();// 回滚事务
                return $this->error('未知错误，审核失败.');
            }
            Db::commit();// 提交事务
        }

	    $data = Db::name('user_region')->where('id',$id)->find();
        if(Db::table('shop_order_info')->where('order_id',$data['order_id'])->where('pay_status',1)->count('order_id')){
            $data['order'] = 1;
        }else{
            $data['order'] = 2;
        }
        if($data['level'] == 1){
            $region_name = Db::name('bs_province')->where('PROVINCE_CODE',$data['region'])->value('PROVINCE_NAME');
            $data['region_name'] = '省代：'.$region_name;
        }elseif($data['level']  == 2){
            $region_name = Db::name('bs_city')->where('CITY_CODE',$data['region'])->value('CITY_NAME');
            $data['region_name'] = '市代：'.$region_name;
        }elseif($data['level']  == 3){
            $region_name = Db::name('bs_area')->where('AREA_CODE',$data['region'])->value('AREA_NAME');
            $data['region_name'] = '区代：'.$region_name;
        }elseif($data['level']  == 4){
            $region_name = Db::name('bs_street')->where('STREET_CODE',$data['region'])->value('STREET_NAME');
            $data['region_name'] = '镇代：'.$region_name;
        }
        $this->assign("row", $data);
        return response($this->fetch('sys_admin/region/region_info'));
//		return $data;
	}
	
	/*------------------------------------------------------ */
	//-- 修改前处理
	/*------------------------------------------------------ */
    public function beforeEdit($data){
//        dump($data);
//        dump(input(''));exit;
        if($data['status'] == ''){
            return $this->error('请选择审核状态');
        }
        if(!$data['id']){
            return $this->error('网络错误，请稍后再试');
        }
		$data['id'] = $data['id'];
		$data['status'] = $data['status'];

        if($data['status'] == 1){
            $proxy = Db::name('users')->where('proxy',$data['region'])->value('proxy');
            if($proxy) return $this->error('该代理已存在.');
             Db::name('users')->where('user_id',$data['user_id'])->update(['proxy'=>$data['region']]);
        }
		Db::startTrans();//启动事务
		return $data;		
	}
	/*------------------------------------------------------ */
	//-- 修改后处理
	/*------------------------------------------------------ */
    public function afterEdit($data){

        $changedata['id'] = $data['id'];
        $changedata['status'] = $data['status'];
        $changedata['update_time'] = time();
        $res = Db::name('user_region')->where('id',$data['id'])->update($changedata);
        if ($res !== false) {
            return $this->success('操作成功.',url('index'));
        }else{
            Db::rollback();// 回滚事务
            return $this->error('未知错误，审核失败.');
        }
		Db::commit();// 提交事务

	}
}
